package ip

import (
	"gitee.com/injoyai/goutil/conv"
	"gitee.com/injoyai/goutil/net/http"
	"gitee.com/injoyai/goutil/string/str"
)

// GetCity 获取ip所属城市
func GetCity(ip string) string {
	if ip == "" {
		return ""
	}
	if ip == "[::1]" || ip == "127.0.0.1" {
		return "内网IP"
	}
	url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
	bytes, _ := http.GetBytes(url)
	result, _ := str.GbkToUtf8(bytes)
	return conv.NewMap(result).GetString("city")
}

// GetLocalCity 获取本机所在城市
func GetLocalCity() string {
	return GetCity(GetNetV4())
}
